package org.jeecg.modules.basedata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.StringUtils;
import org.jeecg.entity.basedata.BaseArea;
import org.jeecg.modules.basedata.mapper.BaseAreaMapper;
import org.jeecg.model.basedata.BaseAreaTree;
import org.jeecg.modules.basedata.service.IBaseAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Description: 区域
 * @Author: jeecg-boot
 * @Date:   2019-06-14
 * @Version: V1.0
 */
@Service
public class BaseAreaServiceImpl extends ServiceImpl<BaseAreaMapper, BaseArea> implements IBaseAreaService {

    @Autowired
    private BaseAreaMapper baseAreaMapper;

    @Override

    public List<BaseAreaTree> getBaseAreaTree(BaseArea baseArea, boolean rootFlag,HttpServletRequest req) {
        QueryWrapper<BaseArea> queryWrapper = QueryGenerator.initQueryWrapper(baseArea, req.getParameterMap());
        List<BaseArea> areaList = list(queryWrapper);
        List<BaseAreaTree> areaTreeList = wrapTreeDataToTreeList(areaList,rootFlag);
        return areaTreeList;
    }

    @Override
    public List<BaseArea> findByParentId(String parentId) {
        BaseArea area = new BaseArea();
        area.setParentId(parentId);
        QueryWrapper<BaseArea> queryWrapper = QueryGenerator.initQueryWrapper(area,new HashMap<>());
        return list(queryWrapper);
    }

    @Override
    public void updateParentIds(List<BaseArea> childList) {
        for (BaseArea baseArea:childList){
            baseAreaMapper.updateParentIds(baseArea);
        }
    }

    public static List<BaseAreaTree> wrapTreeDataToTreeList(List<BaseArea> recordList, boolean rootFlag) {
        // 在该方法每请求一次,都要对全局list集合进行一次清理
        List<BaseAreaTree> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            BaseArea depart = recordList.get(i);
            records.add(new BaseAreaTree(depart));
        }
        if (rootFlag){
            return findRootChildren(records);
        } else {
            return findChildren(records);
        }
//        setEmptyChildrenAsNull(tree);
    }

    private static List<BaseAreaTree> findRootChildren(List<BaseAreaTree> recordList) {
        List<BaseAreaTree> treeList = new ArrayList<>();
        Map<String,String> recordIdMap = new HashMap<>();
        for (int i = 0; i < recordList.size(); i++) {
            BaseAreaTree branch = recordList.get(i);
            recordIdMap.put(branch.getId(),branch.getName());
            if (StringUtils.isBlank(branch.getParentId())) {
                treeList.add(branch);
            }
        }
        //判断是否存在顶级节点
        if (treeList.size() <= 0) {
            for (BaseAreaTree record:recordList) {
                if (recordIdMap.get(record.getParentId()) == null){
                    treeList.add(record);
                }
            }
        }
        getGrandChildren(treeList,recordList);
        return treeList;
    }

    private static List<BaseAreaTree> findChildren(List<BaseAreaTree> recordList) {
        List<BaseAreaTree> treeList = new ArrayList<>();
        Map<String,String> recordIdMap = new HashMap<>();
        for (int i = 0; i < recordList.size(); i++) {
            BaseAreaTree branch = recordList.get(i);
            recordIdMap.put(branch.getId(),branch.getName());
            if ("1".equals(branch.getParentId())) {
                treeList.add(branch);
            }
        }
        //判断是否存在顶级节点
        if (treeList.size() <= 0) {
            for (BaseAreaTree record:recordList) {
                if (recordIdMap.get(record.getParentId()) == null){
                    treeList.add(record);
                }
            }
        }
        getGrandChildren(treeList,recordList);
        return treeList;
    }

    public static void getGrandChildren(List<BaseAreaTree> treeList,List<BaseAreaTree> recordList) {
        if (treeList == null){
            return;
        }
        for (int i = 0; i < treeList.size(); i++) {
            BaseAreaTree model = treeList.get(i);
            for (int i1 = 0; i1 < recordList.size(); i1++) {
                BaseAreaTree m = recordList.get(i1);
                if (m.getParentId()!=null && m.getParentId().equals(model.getId())) {
                    if (model.getChildren() == null){
                        model.setChildren(new ArrayList<BaseAreaTree>());
                    }
                    model.getChildren().add(m);
                }
            }
            getGrandChildren(treeList.get(i).getChildren(), recordList);
        }
    }

//    private List<BaseAreaTree> sortTree(List<BaseAreaTree> areaTreeList) {
//        if (areaTreeList.size() <= 0){
//            return areaTreeList;
//        }
//        List<BaseAreaTree> sortTrees = new LinkedList<>();
//        for (BaseAreaTree treeNode:areaTreeList){
//            if (StringUtils.isBlank(treeNode.getParentId()) ){
//                sortTrees.add(treeNode);
//            }
//        }
//        for (BaseAreaTree treeNode:areaTreeList){
//            for (BaseAreaTree it : areaTreeList) {
//                if (it.getId().equals(treeNode.getId())) continue;
//                if (StringUtils.isBlank(it.getParentId())) continue;
//                if (it.getParentId().equals(treeNode.getId())) {
//                    if (treeNode.getChildren() == null) {
//                        treeNode.setChildren(new ArrayList<>());
//                    }
//                    treeNode.getChildren().add(it);
//                }
//            }
//        }
//        return sortTrees;
//    }
}
